package com.example.demo.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * CountDownLatch,用来控制一个或多个线程等待多个线程
 * 维护了一个奇数器,cnt,每次调用countDown()方法会让计数器的值减1,减到0d shih
 * 那些因为调用await()方法而早等到就会被唤醒
 */
public class demo01 {

    public static void main(String[] args) throws  InterruptedException  {
     final  int  totalThread = 10;
        CountDownLatch countDownLatch = new CountDownLatch(totalThread);
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i <totalThread ; i++) {
            executorService.execute(()->{
                System.out.println("run...");
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        System.out.println("end");
        executorService.shutdown();
    }





}
